/*
 * Created on Feb 16, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package icraf.common.util;

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * <code>FileExcelIO</code> handling file excel operation
 * 
 * @author student4
 * 
 */
public class FileExcelIO {
	public static int WRITE_APPEND = 3;
	public static int WRITE_REPLACE = 4;

	private static WritableWorkbook getWritableWorkbook(File file)
			throws IOException, BiffException {
		Workbook workbook = null;
		WritableWorkbook wb;
		try {
			workbook = Workbook.getWorkbook(file);
		} catch (Exception e) {
			// e.printStackTrace();
		}

		if (workbook != null) {
			// Create new excel file
			wb = Workbook.createWorkbook(file, workbook);
		} else {
			// Open existing excel file
			wb = Workbook.createWorkbook(file);
		}
		return wb;
	}

	/**
	 * open sheet if exist, otherwise create new
	 * 
	 * @param wb
	 * @param sheetName
	 * @return
	 */
	private static WritableSheet getWritableSheet(WritableWorkbook wb,
			String sheetName, int flag) {
		Sheet s[] = wb.getSheets();
		int i;
		for (i = 0; i < s.length; i++) {
			if (s[i].getName().equals(sheetName))
				break;
		}
		WritableSheet sheet;
		if (i < s.length) {
			// open existing sheet
			if (flag == WRITE_REPLACE) {
				wb.removeSheet(i);
				sheet = wb.createSheet(sheetName, i);
			} else {
				sheet = wb.getSheet(i);
			}
		} else {
			// create new sheet
			sheet = wb.createSheet(sheetName, i);
		}
		return sheet;
	}

	/**
	 * write a matrix of double into sheet
	 * 
	 * @param c
	 *            array of double
	 * @param sheetName
	 *            sheet name
	 * @param startOfColumn
	 *            start column on the seet
	 * @param startOfRow
	 * @param flag
	 *            flag appending or overwrite sheet
	 * @return Error Code
	 * @throws WriteException
	 * @throws BiffException
	 */
	public static void writeArray(float c[][], File file, String sheetName,
			int startOfColumn, int startOfRow, int flag) throws IOException,
			WriteException, BiffException {
		WritableWorkbook wb = getWritableWorkbook(file);
		// Find Sheet
		WritableSheet sheet = getWritableSheet(wb, sheetName, flag);
		WritableCell cell;
		for (int i = 0; i < c.length; i++) {
			for (int j = 0; j < c[i].length; j++) {
				cell = new Number(j + startOfColumn, i + startOfRow, c[i][j]);
				sheet.addCell(cell);
			}
		}
		wb.write();
		wb.close();
	}

	/**
	 * write a matrix of string into sheet
	 * 
	 * @param c
	 *            double array of string
	 * @param sheetName
	 *            sheet name
	 * @param startOfColumn
	 * @param startOfRow
	 * @param flag
	 *            indicate appending or overwrite sheet
	 * @throws WriteException
	 * @throws BiffException
	 */
	public static void writeArray(String c[][], File file, String sheetName,
			int startOfColumn, int startOfRow, int flag) throws IOException,
			WriteException, BiffException {
		WritableWorkbook wb = getWritableWorkbook(file);
		// Find Sheet
		WritableSheet sheet = getWritableSheet(wb, sheetName, flag);
		WritableCell cell;
		for (int i = 0; i < c.length; i++) {
			for (int j = 0; j < c[i].length; j++) {
				if (c[i][j] == null)
					continue;
				try {
					double d = Double.parseDouble(c[i][j]);
					cell = new Number(j + startOfColumn, i + startOfRow, d);
				} catch (NumberFormatException e) {
					cell = new Label(j + startOfColumn, i + startOfRow, c[i][j]);
				}
				sheet.addCell(cell);
			}
		}
		wb.write();
		wb.close();
	}

}